package com.aijk.web.admin.utils;

import com.aijk.commons.utils.Utils;
import com.aijk.pe.system.model.SysAdmin;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lenovo on 2016/11/2.
 */
public class CompanyUtil {

    public static void addAuth(HashMap skey, SysAdmin currentUser) {
        if(Utils.isNotEmpty(currentUser.getCompanyId())){
            skey.put("companyId",currentUser.getCompanyId());
        }
        if(Utils.isNotEmpty(currentUser.getSysFlag())){
            skey.put("sysFlag",currentUser.getSysFlag());
        }
    }

    public static <T> T  addAuth(SysAdmin currentUser,Class<T> clazz) {
        try {
            Object bean = clazz.newInstance();
            Field[] field = clazz.getDeclaredFields();//获取基类的属性
            for (int i = 0; i < field.length; i++){
                field[i].setAccessible(true);
                if (Utils.isNotEmpty(field[i].getName())&&field[i].getName().equals("companyId")){
                    field[i].set(bean,currentUser.getCompanyId());
                }
                if (Utils.isNotEmpty(field[i].getName())&&field[i].getName().equals("sysFlag")){
                    field[i].set(bean,currentUser.getSysFlag());
                }
            }
            return (T)bean;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
       return null;
    }
}
